'use strict';

const Service = require('egg').Service;

class SearchService extends Service {

  // 创建用户搜索记录
  async create(payload) {
    const { app, ctx } = this;
    const { keywords } = payload;
    console.log(ctx.state);
    // 判断用户是否登录
    if (ctx.state.user.id) {
      console.log(`=======>用户:${ctx.state.user.nickname} 搜索了:“${keywords}”`);
      const user_id = ctx.state.user.id;

      const isData = await app.model.ArticleSearch.findOne({ where: { user_id, keywords } });
      // r如果用户之前搜索过这个词，就让number搜索次数+1
      if (isData) {
        await isData.increment('number');
        return await isData.reload();
      }
      // 创建一条用户搜索记录
      return await app.model.ArticleSearch.create({ user_id, keywords });
    }
  }

  // 获取用户搜索历史记录
  async getUserHistory(payload) {
    const { app } = this;
    const { pageNum = 1, pageSize = 10, user_id } = payload;
    const where = {};
    if (user_id) { where.user_id = user_id; }
    try {
      const { count, rows } = await app.model.ArticleSearch.findAndCountAll({
        where,
        order: [[ 'updatedAt', 'desc' ]], // 根据创建时间倒序排序
        offset: (pageNum - 1) * pageSize,
        limit: pageSize * 1,
      });
      return {
        pageNum,
        pageSize,
        total: count,
        list: rows,
      };
    } catch (error) {
      console.error(error);
      return null;
    }
  }

  // 获取热门搜索
  async getHotSearch(payload) {
    const { app } = this;
    const { pageNum, pageSize } = payload;
    const where = {};
    try {
      const { count, rows } = await app.model.ArticleSearch.findAndCountAll({
        where,
        order: [[ 'number', 'DESC' ]], // 根据搜索量倒序排序
        offset: (pageNum - 1) * pageSize,
        limit: pageSize * 1,
      });
      return {
        pageNum,
        pageSize,
        total: count,
        list: rows,
      };
    } catch (error) {
      console.error(error);
      return null;
    }
  }


  // 删除单条用户搜索历史记录
  async delRowSearchHistory(id) {
    const { app } = this;
    return await app.model.ArticleSearch.destroy({ where: { id } });
  }
  // 删除用户所有搜索历史记录
  async delAllSearchHistory() {
    const { app, ctx } = this;
    const user_id = ctx.state.user.id;
    return await app.model.ArticleSearch.destroy({ where: { user_id } });
  }

  // 修改
  // async updateArticle(id, category) {
  //   const { app } = this;
  //   return await app.model.Article.update(category, { where: { id } });
  // }

  // 根据id获取信息
  // async getInfo(id) {
  //   const { app } = this;
  //   const res = await app.model.Article.findOne(
  //     {
  //       where: { id },
  //       include: {
  //         model: this.app.model.User,
  //         as: 'user',
  //         attributes: [ 'user_name', 'avatar' ], // 只显示哪些字段
  //       // through: { attributes: [] }, // 排除中间表
  //       },
  //     });
  //   if (res.dataValues) {
  //     // 查询成功的话 就让阅读量加1
  //     await app.model.Article.increment('read_count', { where: { id } });
  //   }
  //   return res.dataValues;
  // }

}

module.exports = SearchService;
